package com.project.weather.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.project.weather.dao.UserRoleMapper;
import com.project.weather.pojo.UserRoleBean;
import com.project.weather.service.UserRoleService;
import com.project.weather.common.PageBean;
import com.project.weather.common.AResultCode;
import com.project.weather.common.ResultMap;
import com.project.weather.utils.StringUtil;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *<p>Title:UserRoleServiceImpl.java</p>
 *<p>Description:用户角色关联表ServiceImpl</p>
 *@author xxx
 *@version 1.0
 *@Automatically generate by Coder in 2021-12-21 16:02
 */
@Service
public class UserRoleServiceImpl implements UserRoleService{
	private static final Logger log = LoggerFactory.getLogger(UserRoleServiceImpl.class);
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	@Override
	public ResultMap<AResultCode, UserRoleBean> insert(UserRoleBean bean){
		ResultMap<AResultCode, UserRoleBean> result = new ResultMap<AResultCode, UserRoleBean>();
		if(StringUtil.isNull(bean)){
			result.setCode(AResultCode.IS_NULL);
		} else {
//			if(StringUtil.isNull(bean.getId())){
//				bean.setId(StringUtil.getUUID());
//			}
			userRoleMapper.insertSelective(bean);	
			result.setCode(AResultCode.OK);
			result.setResult(bean);
		}
		return result;
	}

	
	@Override
	public ResultMap<AResultCode, UserRoleBean> update(UserRoleBean bean){
		ResultMap<AResultCode, UserRoleBean> result = new ResultMap<AResultCode, UserRoleBean>();

		if(StringUtil.isNull(bean)){
			result.setCode(AResultCode.IS_NULL);
		} else if(StringUtil.isNull(bean.getId())){
			result.setCode(AResultCode.MISS_ID);
		} else {
			userRoleMapper.updateSelective(bean);	
			result.setCode(AResultCode.OK);
			result.setResult(bean);
		}

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, UserRoleBean> delete(String id){
		ResultMap<AResultCode, UserRoleBean> result = new ResultMap<AResultCode, UserRoleBean>();

		if(StringUtil.isNull(id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			String[] ids = id.split(",");
			userRoleMapper.delete(ids);
			result.setCode(AResultCode.OK);
		}

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, PageBean<UserRoleBean>> pageList(PageBean<UserRoleBean> pb){
		ResultMap<AResultCode, PageBean<UserRoleBean>> result = new ResultMap<AResultCode, PageBean<UserRoleBean>>();

		pb.setData(userRoleMapper.pageList(pb));
		pb.clear();

		result.setCode(AResultCode.OK);
		result.setResult(pb);

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, List<UserRoleBean>> list(UserRoleBean bean){
		ResultMap<AResultCode, List<UserRoleBean>> result = new ResultMap<AResultCode, List<UserRoleBean>>();

		List<UserRoleBean> list = userRoleMapper.list(bean);
		result.setCode(AResultCode.OK);
		result.setResult(list);

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, UserRoleBean> get(String id){
		ResultMap<AResultCode, UserRoleBean> result = new ResultMap<AResultCode, UserRoleBean>();
		if(StringUtil.isNull(id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			UserRoleBean bean = userRoleMapper.get(id);
			if(bean != null) {
				result.setCode(AResultCode.OK);
				result.setResult(bean);
			} else {
				result.setCode(AResultCode.UNEXIST);
	 		}
		}
		return result;
	}

	
}